package cn.imvc.app.controllers.info;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.domains.IndexDataType;
import cn.imvc.app.domains.ShowType;
import cn.imvc.app.entities.IndexClass;
import cn.imvc.app.entities.IndexData;
import cn.imvc.app.models.IndexClassModel;
import cn.imvc.app.models.IndexDataModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class IndexDataControllerImpl implements IndexDataController {

	@Autowired
	IndexDataModel indexDataModel;
	@Autowired
	IndexClassModel indexClassModel;
	@Autowired
	LoggingModel loggingModel;
	static Logger logger = Logger.getLogger(IndexDataControllerImpl.class);
	public Page<IndexData> page;
	public IndexData indexData;

	public ViewRequest viewRequest;
	public int id;
	public int[] ids;
	public Map<Integer, String> showType = ShowType.elMap;
	public Map<Byte, String> indexDataType = IndexDataType.elMap;
	public int indexClassId;
	
	public String search() {
		// TODO Auto-generated method stub
		return null;
	}

	public String index() {
		Map<String, String> searchMap = new HashMap<String, String>();
		if(viewRequest != null){
			searchMap = viewRequest.getParams();
		}else{
			viewRequest = new ViewRequest();
		}
		searchMap.put(" _and_i_dot_indexClass_dot_id_eq_string", String.valueOf(indexClassId));
		viewRequest = new ViewRequest();
		viewRequest.setParams(searchMap);
		viewRequest.getSort().put("sort","ASC");
		page=indexDataModel.find(viewRequest);
		return "index-data/index";
	}

	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

	public String editNew() {
		IndexClass indexClassTmp = indexClassModel.get(indexClassId);
		indexData = new IndexData();
		indexData.setIndexClass(indexClassTmp);
		return "index-data/edit";
	}

	public String create() {
		indexDataModel.save(indexData);
		loggingModel.info(logger,"添加首页内容："+indexData.getTitle());
		return Render.json(new DialogBean("200", "创建成功","index_data", "closeCurrent"));
	}

	public String edit() {
		indexData = indexDataModel.get(id);
		return "index-data/edit";
	}

	public String update() {
		indexDataModel.update(id,indexData);
		loggingModel.info(logger,"修改首页内容："+indexData.getTitle());
		return Render.json(new DialogBean("200", "修改成功","index_data", "closeCurrent"));
	}

	public String destroy() {
		indexDataModel.delete(id);
		loggingModel.info(logger,"删除首页内容："+id);
		return Render.json(new DialogBean("200", "删除成功","index_data"));
	}

	public String destroyBatch() {
		indexDataModel.deleteBatch(ids);
		return Render.json(new DialogBean("200", "删除成功","index_data"));
	}
	public void setIndexDataModel(IndexDataModel indexDataModel) {
		this.indexDataModel = indexDataModel;
	}


	public void setPage(Page<IndexData> page) {
		this.page = page;
	}

	public void setIndexData(IndexData indexData) {
		this.indexData = indexData;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public Map<Integer, String> getShowType() {
		return showType;
	}

	public void setShowType(Map<Integer, String> showType) {
		this.showType = showType;
	}

	public Map<Byte, String> getIndexDataType() {
		return indexDataType;
	}

	public void setIndexDataType(Map<Byte, String> indexDataType) {
		this.indexDataType = indexDataType;
	}

	public int getIndexClassId() {
		return indexClassId;
	}

	public void setIndexClassId(int indexClassId) {
		this.indexClassId = indexClassId;
	}
}
